package com.xiaohai.big_event_system.aop;

import com.xiaohai.big_event_system.annotation.LogExecutionTime;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class SpendTime {

    /* @Pointcut("execution(* com.xiaohai.big_event_system.controller.*.*(..)))")
     public void point() {
     }
     @Around("point()")*/


    /*  @Around("execution(* com.xiaohai.big_event_system.controller.*.*(..)))")*/
    @Around("@annotation(logExecutionTime)")
    public Object spendTime(ProceedingJoinPoint joinPoint, LogExecutionTime logExecutionTime) throws Throwable {
        // 读取注解LogExecutionTime的自定义属性
        String description = logExecutionTime.description();
        log.info("方法{}开始执行", description);

        String name = joinPoint.getSignature().getName();
        long startTime = System.currentTimeMillis();
        Object object = joinPoint.proceed();
        long end = System.currentTimeMillis();
        log.info("方法{}执行时间：{}毫秒", name, end - startTime);
        return object;
    }
}
